/*
 * Orderform Action
 */
var CKL     = require("../utils/checklogin"),
Response    = require("../utils/response"),
DaoMysql    = require('../models/dao-mysql').DaoMysql,
CacheRedis  = require('../models/cache-redis').CacheRedis,
Orderform   = require('../models/orderform').Orderform;
Driver      = require('../models/driver').Driver;
Passenger   = require('../models/passenger').Passenger;
Page        = require('../models/page').Page;

module.exports = function(app)
{
	var dao = new DaoMysql(app.envConfig.dbMysql, app.mysqlClient, app.logmessage, new CacheRedis(app.envConfig.storeRedis, app.redisClient, app.logmessage, app.defs.DEFAULT_CACHE_EXPIRE_SECS));
	
	//订单列表get请求，验证是否登录
	app.get(app.envConfig.routes.orderform_user_list.url,CKL.logined);
	app.get(app.envConfig.routes.orderform_user_list.url,function(req,res){
		orderformList(req,res);
	});
	
	//订单列表post请求，验证是否登录
	app.post(app.envConfig.routes.orderform_user_list.url,CKL.logined);
	app.post(app.envConfig.routes.orderform_user_list.url,function(req,res){
		orderformList(req,res);
	});
	
	
	//订单详情
	app.get(app.envConfig.routes.orderform_user_detail.url,CKL.logined);
	app.get(app.envConfig.routes.orderform_user_detail.url,function(req,res){
		if(req.params.id){
				dao.get(Orderform,req.params.id,function(err,result){
					if(err){
						msg(err);
					}
					
					dao.get(Passenger,result.user_id,function(err,resultPassenger){
						result.userName = resultPassenger.realname;
					});

					dao.get(Driver,result.taxi_id,function(err,resultDriver){
						result.driverName = resultDriver.realname;
						Response.render(app,res,app.envConfig.routes.orderform_user_detail.page,{user:req.session.user,result:result});
					});
				});
		}
	});
	
	//订单分页查询方法
	function orderformList(req,res){
		
		var id = req.params.id;
		var type = parseInt(req.params.type);
		var sql = "";
		var userName = "";
		var driverName = "";
		if(type==0)
		{
			sql = 'user_id="'+id+'"';
			dao.get(Passenger,id,function(err,result){
				userName = result.realname;
			});
		}
		else
		{
			sql = 'taxi_id="'+id+'"';
			dao.get(Driver,id,function(err,result){
				driverName = result.realname;
			});
		}
		
		if(!req.body.pageNo){
			req.body.pageNo=0;
		}
		if(!req.body.pageSize){
			req.body.pageSize=5;
		}
		dao.searchPage(Orderform,sql,req.body.pageNo+","+req.body.pageSize,function(err,result,total){
			if(err){
				msg(err);
			}
			if(!result || result.length==0){
				Response.render(app,res,app.envConfig.routes.orderform_user_list.page,{user:req.session.user,page:{}});
			}
			for(var i=0; i<result.length; i++)
			{
				var object = result[i];
				if(type==0){
					object.userName = userName;
					dao.get(Driver,object.taxi_id,function(err,resultDriver){
						var j = result.length-i;
						i = i-1;
						result[j].driverName = resultDriver.realname;
						if(i==0)
						{
							var page=new Page(req.body.pageNo,req.body.pageSize,result,total);
							Response.render(app,res,app.envConfig.routes.orderform_user_list.page,{user:req.session.user,page:page});
						}
					});
				}else{
					object.driverName = driverName;
					dao.get(Passenger,object.user_id,function(err,resultPassenger){
						var j = result.length-i;
						i = i-1;
						result[j].userName = resultPassenger.realname;
						if(i==0)
						{
							var page=new Page(req.body.pageNo,req.body.pageSize,result,total);
							Response.render(app,res,app.envConfig.routes.orderform_user_list.page,{user:req.session.user,page:page});
						}
					});
				}
			}
		});
	}
	
};